package stream;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StreamUtil {
    static String path = null;
    static File file = null;
    static FileInputStream fis = null;
    // static {
    // path = "d:" + File.separator + "aaa.txt";
    // file = new File(path);
    // try {
    // fis = new FileInputStream(file);
    // } catch (FileNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }

    public static void setPath(String path) {
        file = new File(path);
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        byte[] b = new byte[10];
        int len = -1;
        while ((len = fis.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
            System.out.println("end:" + len);
        }
        // fileInputStream();
        // fileOutputStream();
        // fileCopy();
        // dataInputStream();
    }

    public static void fileInputStream() {
        byte[] b = new byte[1024];
        int len = 0;
        StringBuffer sb = new StringBuffer();
        try {
            while ((len = fis.read(b)) != -1) {
                sb.append(new String(b, 0, len));
                System.out.println("ff: " + new String(b, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sb.toString());
    }

    public static String outPutFile(String path) {
        File file = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] b = new byte[1024];
        int len = 0;
        StringBuffer sb = new StringBuffer();
        try {
            while ((len = fis.read(b)) != -1) {
                sb.append(new String(b, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void fileOutputStream(String log) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file, true);
            fos.write(log.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void fileCopy() {
        File file2 = new File("d:" + file.separator + "aaa2.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file2, true);
            byte[] b = new byte[255];
            int key = 0;
            while ((key = fis.read(b)) != -1) {
                fos.write(b);
            }
            // fos.write(data.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void fileCopy(String targetPath, String originalPath, boolean append) throws IOException {
        FileInputStream originStream = new FileInputStream(new File(originalPath));
        FileOutputStream targetStream = new FileOutputStream(new File(targetPath), append);
        byte[] b = new byte[originStream.available()];
        while ((originStream.read(b)) != -1) {
            targetStream.write(b);
        }
        if (originStream != null) {
            originStream.close();
        }
        if (targetStream != null) {
            targetStream.close();
        }
    }

    public static void dataInputStream() {
        DataInputStream dis = new DataInputStream(fis);
        char[] ch = new char[10];
        int count = 0;
        char temp;
        try {
            while ((temp = dis.readChar()) != 'C') {
                ch[count++] = temp;
            }
            System.out.println(ch);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
